/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：Q2048Screen.java <br>
 * 创建时间：2014-6-21 下午20:15:11 <br>
 * 所属项目：GmudEX <br>
 * @author ubun <br>
 */
package lostland.gmud.exv2.expand.minigame;

import java.util.ArrayList;
import java.util.Arrays;

import lostland.gmud.exv2.Assets;
import lostland.gmud.exv2.Direction;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.blgframework.impl.BLGPixmap;
import lostland.gmud.exv2.expand.Matrix;
import lostland.gmud.exv2.ui.NotificationScreen;
import lostland.gmud.exv2.ui.core.ButtonControlledScreen;
import lostland.gmud.exv2.ui.core.NewButton;

/**
 * 类名：Q2048Screen <br>
 * 说明：2048
 *
 * @author ubun
 */
public class Q2048Screen extends ButtonControlledScreen {

	final int line[] = {0, 39, 78, 117, 156};

	boolean isStart = false;
	String s = "";
	Direction d = Direction.DOWN;
	int step = 0;

	int score = 0;
	int maxsc = 2;
	//自定义数据结构：矩阵
	Matrix vov = new Matrix(4, 4);
	Matrix vov_undo = new Matrix(4, 4);

	/**
	 * @param instance
	 */
	public Q2048Screen() {
		super();
		isStart = false;
		vov.fill(0);
		vov_undo.fill(-1);
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#onButtonDown(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	protected void onButtonDown(NewButton b) {
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#onButtonClick(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	public void onButtonClick(NewButton b) {

		switch (b) {
			case NB_MENU:
			/*0,0 1,0 2,0 3,0
			 **0,1 1,1 2,1 3,1
			 **0,2 1,2 2,2 3,2
			 **0,3 1,3 2,3 3,3*/
			case NB_BACK:
				isStart = false;
				getGame().popToRootScreen();
				return;
			case NB_ENTER:
				init1();
				break;
			case NB_DOWN:
				init1();
				//空格子处理
				for (int i = 0; i < 4; i++) {
					int vv[] = new int[4];
					int o = 3;
					Arrays.fill(vv, 0);
					for (int j = 3; j >= 0; j--) {
						if (vov.get(i, j) != 0) {
							vv[o] = vov.get(i, j);
							o--;
						}
					}
					vov.fillcolumn(i, vv);
				}
				//从下往上分析
				for (int i = 0; i < 4; i++) {
					for (int j = 3; j > 0; j--) {
						if (vov.get(i, j) == vov.get(i, j - 1)) {
							//一样格子的时候合并
							vov.set(i, j, vov.get(i, j) * 2);
							for (int k = j - 1; k > 0; k--) {
								//合并后下移
								vov.set(i, k, vov.get(i, k - 1));
								vov.set(i, k - 1, 0);
							}
						}
					}
				}
				updateScore();
				newCase();
				break;
			case NB_UP:
				init1();
				//空格子处理
				for (int i = 0; i < 4; i++) {
					int vv[] = new int[4];
					int o = 0;
					Arrays.fill(vv, 0);
					for (int j = 0; j < 4; j++) {
						if (vov.get(i, j) != 0) {
							vv[o] = vov.get(i, j);
							o++;
						}
					}
					vov.fillcolumn(i, vv);
				}
				//从上往下分析
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 3; j++) {
						if (vov.get(i, j) == vov.get(i, j + 1)) {
							//一样格子的时候合并
							vov.set(i, j, vov.get(i, j) * 2);
							for (int k = j + 1; k < 3; k++) {
								vov.set(i, k, vov.get(i, k + 1));
								vov.set(i, k + 1, 0);
							}
						}
					}
				}
				updateScore();
				newCase();
				break;
			case NB_LEFT:
				init1();
				//空格子处理
				for (int j = 0; j < 4; j++) {
					int vv[] = new int[4];
					int o = 0;
					Arrays.fill(vv, 0);
					for (int i = 0; i < 4; i++) {
						if (vov.get(i, j) != 0) {
							vv[o] = vov.get(i, j);
							o++;
						}
					}
					vov.fillrow(j, vv);
				}
				//从左往右分析
				for (int j = 0; j < 4; j++) {
					for (int i = 0; i < 3; i++) {
						if (vov.get(i, j) == vov.get(i + 1, j)) {
							//一样格子的时候合并
							vov.set(i, j, vov.get(i, j) * 2);
							for (int k = i + 1; k < 3; k++) {
								vov.set(k, j, vov.get(k + 1, j));
								vov.set(k + 1, j, 0);
							}
						}
					}
				}
				updateScore();
				newCase();
				break;
			case NB_RIGHT:
				init1();
				//空格子处理
				for (int j = 0; j < 4; j++) {
					int vv[] = new int[4];
					int o = 3;
					Arrays.fill(vv, 0);
					for (int i = 3; i >= 0; i--) {
						if (vov.get(i, j) != 0) {
							vv[o] = vov.get(i, j);
							o--;
						}
					}
					vov.fillrow(j, vv);
				}
				//从右往左分析
				for (int j = 0; j < 4; j++) {
					for (int i = 3; i > 0; i--) {
						if (vov.get(i, j) == vov.get(i - 1, j)) {
							//一样格子的时候合并
							vov.set(i, j, vov.get(i, j) * 2);
							for (int k = i - 1; k > 0; k--) {
								vov.set(k, j, vov.get(k - 1, j));
								vov.set(k - 1, j, 0);
							}
						}
					}
				}
				updateScore();
				newCase();
				break;
			case NB_NULL:
			default:
				break;
		}
	}

	protected void init1() {
		if (!isStart) {
			newCase();
			newCase();
			isStart = true;
		}
	}

	void updateScore() {
		//分数更新
		int sc = 0;
		int xsc = 2;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (vov.get(i, j) > xsc) {
					xsc = vov.get(i, j);
				}
				if (vov.get(i, j) > 2) {
					sc += vov.get(i, j);
				}
			}
		}
		maxsc = xsc;
		if (xsc > 255) {
			s = "最大数字：" + xsc;
		}
		score = sc;
		if (score > Game.getData().q2048_best) {
			Game.getData().q2048_best = score;
		}
	}

	void newCase() {
		//如果没改变就不增加新格
//		GmudGame.logError("EQUAL", vov.print() + " <--> " + vov_undo.print());
		if (vov.equals(vov_undo))
			return;

		//获得新的位置
		ArrayList<Integer> black_case = new ArrayList<>();
		for (int i = 0; i < vov.getData().length; i++) {
			if (vov.getData()[i] <= 0) {
				black_case.add(i);
			}
		}
		int p = !black_case.isEmpty() ? (int) (Math.random() * black_case.size()) : -1;

		//填满判断
		if (p < 0) {
//			cancel();
			new NotificationScreen("你在2048游戏中获得了" + score + "分。").pushToGame();
			return;
		}

		int r = black_case.get(p) / 4;
		int c = black_case.get(p) % 4;

		if ((int) (Math.random() * 3) == 0) {
			vov.set(c, r, 4);
		} else {
			vov.set(c, r, 2);
		}

		vov_undo.clonefrom(vov);
	}

	String int2String(int i) {
		return i > 0 ? String.valueOf(i) : "";
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#show()
	 */
	@Override
	protected void show() {
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();

		g.clear(Gmud.getBgColor());

		//大边框
		g.drawLine(0, 0, GameConstants.FBWIDTH, 0, 0);
		g.drawLine(0, 0, 0, GameConstants.FBHEIGHT, 0);
		g.drawLine(GameConstants.FBWIDTH, 0, GameConstants.FBWIDTH, GameConstants.FBHEIGHT, 0);
		g.drawLine(0, GameConstants.FBHEIGHT, GameConstants.FBWIDTH, GameConstants.FBHEIGHT, 0);

		//格子边框
		g.drawLine(line[4], 0, line[4], line[4], 0);
		g.drawLine(0, line[4], GameConstants.FBWIDTH, line[4], 0);

		//内部线条
		g.drawLine(0, line[1], line[4], line[1], 0);
		g.drawLine(0, line[2], line[4], line[2], 0);
		g.drawLine(0, line[3], line[4], line[3], 0);
		g.drawLine(line[1], 0, line[1], line[4], 0);
		g.drawLine(line[2], 0, line[2], line[4], 0);
		g.drawLine(line[3], 0, line[3], line[4], 0);

		//格子数值
		int ij[] = {15, 54, 93, 132};
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
//				g.drawText(i + "," + j, ij[i], ij[j], FontSize.FT_16PX);
				g.drawText(int2String(vov.get(i, j)), ij[i], ij[j], FontSize.FT_16PX);
			}
		}

		g.drawText("SCORE:" + score, 24, 162, FontSize.FT_16PX);
		g.drawText("BEST:" + Game.getData().q2048_best, 180, 162, FontSize.FT_16PX);

		final int MAINCHAR_WIDTH = 32;
		final int MAINCHAR_HEIGHT = 48;

		BLGPixmap pixmap = (BLGPixmap) Assets.getMainchar();
		if (Gmud.mc.sex > 0) {
			pixmap = (BLGPixmap) Assets.getGirl();
		}

		g.drawPixmap(pixmap, 204, 24, MAINCHAR_WIDTH * 2, MAINCHAR_HEIGHT * 2
				, (step % 4) * MAINCHAR_WIDTH, d.maincharTileY() * MAINCHAR_HEIGHT, MAINCHAR_WIDTH, MAINCHAR_HEIGHT);

		g.draw2Text(s, 157, 1, FontSize.FT_12PX);
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#update(float)
	 */
//	@Override
//	public void update(float deltaTime) {
//		super.update(deltaTime);
//		if(isEnd)
//			cancel();
//	}

	void cancel() {
		isStart = false;
		if (score > 2048 && maxsc >= 2048) {
			int level = Math.min(240 - Gmud.mc.skills[50], (score - 1024) / 200);
			Gmud.mc.skills[50] = level;
			getGame().popToRootScreen();
			new NotificationScreen("你在2048游戏中获得了" + score + "分，你领悟了" + level + "级乾坤大挪移！").pushToGame();
		} else if (score > 1024 && maxsc == 1024) {
			Gmud.mc.skills[47] = 1;
			getGame().popToRootScreen();
			new NotificationScreen("你在2048游戏中获得了" + score + "分，领悟了鹰爪功！").pushToGame();
		} else {
			getGame().popToRootScreen();
			new NotificationScreen("你在2048游戏中获得了" + score + "分。").pushToGame();
		}
	}
}
